package com.substanceofcode.data.midp;

import java.util.Enumeration;
import java.util.Vector;

import com.substanceofcode.data.FileSystem;
import com.substanceofcode.data.midp.RsfsFileTest.DummyFile;

import net.sf.microlog.core.Logger;
import net.sf.microlog.core.LoggerFactory;

import junitj2me.framework.TestCase;

public class RsfsListTest extends TestCase
{
    private final static Logger LOG = LoggerFactory.getLogger();

    protected void setUp() throws Exception
    {
        FileSystemImpl.resetStaticData();
        FileSystemImpl.getFileSystem().formatFileSystem();
    }

    private static final String TEST_FILENAME = "file:////somedirectory/another/bigtestfile";

    public void test1()
    {
        LOG.info( "testListAll" );

        try
        {
            FileSystemImpl fileSystemImpl = FileSystemImpl.getFileSystem();

            assertEquals( "iCurrentRecordStoreNumber 1", 1, fileSystemImpl.iCurrentRecordStoreNumber );
            //assertEquals( "iRecordsInCurrentRecordStore 0", 0, fileSystem.iRecordsInCurrentRecordStore );

            for( int i = 0; i < 10; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );

                fileSystemImpl.saveFile( TEST_FILENAME + i, dummyFile, true );

                RsfsFileTest.assertFileContentsEqual( TEST_FILENAME + i, Integer.toString( i ).getBytes() );
            }

            Enumeration allFileNames1 = fileSystemImpl.listFiles();
            int allFileNames1Size = 0;
            // Make sure all files returned exist are present.
            while( allFileNames1.hasMoreElements() )
            {
                String fileName = (String) allFileNames1.nextElement();
                ++allFileNames1Size;

                assertTrue( fileSystemImpl.exists( fileName ) );

                if( fileName.startsWith( TEST_FILENAME ) )
                {
                    // No guarantee about order, so strip the number off to look for.
                    String numberString = fileName.substring( TEST_FILENAME.length(), TEST_FILENAME.length() + 1 );
                    int number = Integer.parseInt( numberString );
                    RsfsFileTest.assertFileContentsEqual( fileName, Integer.toString( number ).getBytes() );
                }
                else
                {
                    fail( "Unexpected filename" );
                }
            }

            assertEquals( 10, allFileNames1Size );

            FileSystemImpl.resetStaticData();

            Enumeration allFileNames2 = fileSystemImpl.listFiles();
            int allFileNames2Size = 0;
            // Make sure all files returned exist are present.
            while( allFileNames2.hasMoreElements() )
            {
                String fileName = (String) allFileNames2.nextElement();
                ++allFileNames2Size;

                assertTrue( fileSystemImpl.exists( fileName ) );

                if( fileName.startsWith( TEST_FILENAME ) )
                {
                    // No guarantee about order, so strip the number off to look for.
                    String numberString = fileName.substring( TEST_FILENAME.length(), TEST_FILENAME.length() + 1 );
                    int number = Integer.parseInt( numberString );
                    RsfsFileTest.assertFileContentsEqual( fileName, Integer.toString( number ).getBytes() );
                }
                else
                {
                    fail( "Unexpected filename" );
                }
            }

            assertEquals( 10, allFileNames2Size );

        }
        catch( Exception e )
        {
            fail( e.getMessage() );
        }
    }

    public void test2()
    {
        LOG.info( "testListOnlySpecifiedType" );

        try
        {
            FileSystemImpl fileSystemImpl = FileSystemImpl.getFileSystem();

            assertEquals( "iCurrentRecordStoreNumber 1", 1, fileSystemImpl.iCurrentRecordStoreNumber );
            //assertEquals( "iRecordsInCurrentRecordStore 0", 0, fileSystem.iRecordsInCurrentRecordStore );

            for( int i = 0; i < 5; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );

                fileSystemImpl.saveFile( TEST_FILENAME + i, dummyFile, true );

                RsfsFileTest.assertFileContentsEqual( TEST_FILENAME + i, Integer.toString( i ).getBytes() );
            }

            Enumeration allFileNames = fileSystemImpl.listFiles();
            int allFileNamesSize = 0;
            // Make sure all files returned exist are present.
            while( allFileNames.hasMoreElements() )
            {
                String fileName = (String) allFileNames.nextElement();
                ++allFileNamesSize;

                assertTrue( fileSystemImpl.exists( fileName ) );

                if( fileName.startsWith( TEST_FILENAME ) )
                {
                    // No guarantee about order, so strip the number off to look for.
                    String numberString = fileName.substring( TEST_FILENAME.length(), TEST_FILENAME.length() + 1 );
                    int number = Integer.parseInt( numberString );
                    RsfsFileTest.assertFileContentsEqual( fileName, Integer.toString( number ).getBytes() );
                }
                else
                {
                    fail( "Unexpected filename" );
                }
            }

            assertEquals( 5, allFileNamesSize );

            for( int i = 5; i < 10; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "image" );

                fileSystemImpl.saveFile( TEST_FILENAME + i, dummyFile, true );

                RsfsFileTest.assertFileContentsEqual( TEST_FILENAME + i, Integer.toString( i ).getBytes() );
            }

            allFileNames = fileSystemImpl.listFiles();
            // Make sure all files returned exist are present.
            allFileNamesSize = 0;
            while( allFileNames.hasMoreElements() )
            {
                String fileName = (String) allFileNames.nextElement();
                ++allFileNamesSize;

                assertTrue( fileSystemImpl.exists( fileName ) );

                if( fileName.startsWith( TEST_FILENAME ) )
                {
                    // No guarantee about order, so strip the number off to look for.
                    String numberString = fileName.substring( TEST_FILENAME.length(), TEST_FILENAME.length() + 1 );
                    int number = Integer.parseInt( numberString );
                    RsfsFileTest.assertFileContentsEqual( fileName, Integer.toString( number ).getBytes() );
                }
                else
                {
                    fail( "Unexpected filename" );
                }
            }

            assertEquals( 10, allFileNamesSize );

            FileSystemImpl.resetStaticData();

            Enumeration textFileNames = fileSystemImpl.listFilesOfType( "text" );
            // Make sure all files returned exist are present.
            int textFileNamesSize = 0;
            while( textFileNames.hasMoreElements() )
            {
                String fileName = (String) textFileNames.nextElement();
                ++textFileNamesSize;

                assertTrue( fileSystemImpl.exists( fileName ) );

                if( fileName.startsWith( TEST_FILENAME ) )
                {
                    // No guarantee about order, so strip the number off to look for.
                    String numberString = fileName.substring( TEST_FILENAME.length(), TEST_FILENAME.length() + 1 );
                    int number = Integer.parseInt( numberString );
                    RsfsFileTest.assertFileContentsEqual( fileName, Integer.toString( number ).getBytes() );
                }
                else
                {
                    fail( "Unexpected filename" );
                }
            }

            assertEquals( 5, textFileNamesSize );

            Enumeration imageFileNames = fileSystemImpl.listFilesOfType( "image" );
            // Make sure all files returned exist are present.
            int imageFileNamesSize = 0;
            while( imageFileNames.hasMoreElements() )
            {
                String fileName = (String) imageFileNames.nextElement();
                ++imageFileNamesSize;

                assertTrue( fileSystemImpl.exists( fileName ) );

                if( fileName.startsWith( TEST_FILENAME ) )
                {
                    // No guarantee about order, so strip the number off to look for.
                    String numberString = fileName.substring( TEST_FILENAME.length(), TEST_FILENAME.length() + 1 );
                    int number = Integer.parseInt( numberString );
                    RsfsFileTest.assertFileContentsEqual( fileName, Integer.toString( number ).getBytes() );
                }
                else
                {
                    fail( "Unexpected filename" );
                }
            }

            assertEquals( 5, imageFileNamesSize );

        }
        catch( Exception e )
        {
            fail( e.getMessage() );
        }
    }

    private static final String TEST_COMMON_DIRECTORY = "file:////account1/folder1/";

    private static final String TEST_DIRECTORY1       = TEST_COMMON_DIRECTORY + "email1/";
    private static final String TEST_DIRECTORY2       = TEST_COMMON_DIRECTORY + "email2/";

    private static final String TEST_FILEINDIRECTORY1 = "fileindirone";
    private static final String TEST_FILEINDIRECTORY2 = "fileindirtwo";

    public void test3()
    {
        LOG.info( "testListFilesInDirectoryAllImmediatelyUnder" );

        try
        {
            FileSystem fileSystem = FileSystemImpl.getFileSystem();

            for( int i = 0; i < 5; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );
                fileSystem.saveFile( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 + i, dummyFile, true );
                RsfsFileTest.assertFileContentsEqual( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 + i, Integer.toString( i ).getBytes() );
            }

            for( int i = 5; i < 10; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );
                fileSystem.saveFile( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 + i, dummyFile, true );
                RsfsFileTest.assertFileContentsEqual( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 + i, Integer.toString( i ).getBytes() );
            }

            FileSystemImpl.resetStaticData();

            Enumeration directory1FileNames = fileSystem.listFilesInDirectory( TEST_DIRECTORY1 );

            // Make sure all files returned exist are present.
            int numberOfDirectory1FileNames = 0;
            while( directory1FileNames.hasMoreElements() )
            {
                String fileName = (String) directory1FileNames.nextElement();
                ++numberOfDirectory1FileNames;

                assertTrue( fileSystem.exists( fileName ) );

                if( fileName.startsWith( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 ) )
                {
                    // No guarantee about order, so strip the number off to look for.
                    String numberString = fileName.substring( ( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 ).length(), ( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 ).length() + 1 );
                    int number = Integer.parseInt( numberString );
                    RsfsFileTest.assertFileContentsEqual( fileName, Integer.toString( number ).getBytes() );
                }
                else
                {
                    fail( "Unexpected filename" );
                }
            }

            Enumeration directory2FileNames = fileSystem.listFilesInDirectory( TEST_DIRECTORY2 );
            // Make sure all files returned exist are present.
            int numberOfDirectory2FileNames = 0;
            while( directory2FileNames.hasMoreElements() )
            {
                String fileName = (String) directory2FileNames.nextElement();
                ++numberOfDirectory2FileNames;

                assertTrue( fileSystem.exists( fileName ) );

                if( fileName.startsWith( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 ) )
                {
                    // No guarantee about order, so strip the number off to look for.
                    String numberString = fileName.substring( ( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 ).length(), ( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 ).length() + 1 );
                    int number = Integer.parseInt( numberString );
                    RsfsFileTest.assertFileContentsEqual( fileName, Integer.toString( number ).getBytes() );
                }
                else
                {
                    fail( "Unexpected filename" );
                }
            }

            assertEquals( 5, numberOfDirectory2FileNames );

        }
        catch( Exception e )
        {
            fail( e.getMessage() );
        }
    }

    public void test4()
    {
        LOG.info( "testListFilesInHigherDirectoryNoFilesImmediatelyUnder" );

        try
        {
            FileSystem fileSystem = FileSystemImpl.getFileSystem();

            for( int i = 0; i < 5; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );
                fileSystem.saveFile( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 + i, dummyFile, true );
                RsfsFileTest.assertFileContentsEqual( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 + i, Integer.toString( i ).getBytes() );
            }

            for( int i = 5; i < 10; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );
                fileSystem.saveFile( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 + i, dummyFile, true );
                RsfsFileTest.assertFileContentsEqual( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 + i, Integer.toString( i ).getBytes() );
            }

            FileSystemImpl.resetStaticData();

            Enumeration directoryFileNames = fileSystem.listFilesInDirectory( TEST_COMMON_DIRECTORY );

            // We expect the directory listing to contain precisely the two subdirectories:
            //
            //  "file:////account1/folder1/email1/" 
            // and 
            // 
            //  "file:////account1/folder1/email2/".
            //

            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName1 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName2 = (String) directoryFileNames.nextElement();
            assertFalse( directoryFileNames.hasMoreElements() );

            if( fileName1.equals( TEST_DIRECTORY1 ) )
            {
                assertEquals( TEST_DIRECTORY1, fileName1 );
                assertEquals( TEST_DIRECTORY2, fileName2 );
            }
            else if( fileName2.equals( TEST_DIRECTORY1 ) )
            {
                assertEquals( TEST_DIRECTORY1, fileName2 );
                assertEquals( TEST_DIRECTORY2, fileName1 );
            }
            else
            {
                fail( "Should have been email1/ and email2/" );
            }

        }
        catch( Exception e )
        {
            fail( e.getMessage() );
        }
    }

    public void test5()
    {
        LOG.info( "testListFilesInHigherDirectoryAndSomeFilesImmediatelyUnder" );

        try
        {
            FileSystem fileSystem = FileSystemImpl.getFileSystem();

            for( int i = 0; i < 5; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );
                fileSystem.saveFile( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 + i, dummyFile, true );
                RsfsFileTest.assertFileContentsEqual( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 + i, Integer.toString( i ).getBytes() );
            }

            for( int i = 5; i < 10; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );
                fileSystem.saveFile( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 + i, dummyFile, true );
                RsfsFileTest.assertFileContentsEqual( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 + i, Integer.toString( i ).getBytes() );
            }

            DummyFile dummyFile1 = new DummyFile( "Hello".getBytes(), "text" );
            fileSystem.saveFile( TEST_COMMON_DIRECTORY + "File1", dummyFile1, true );
            RsfsFileTest.assertFileContentsEqual( TEST_COMMON_DIRECTORY + "File1", "Hello".getBytes() );

            DummyFile dummyFile2 = new DummyFile( "There".getBytes(), "text" );
            fileSystem.saveFile( TEST_COMMON_DIRECTORY + "File2", dummyFile2, true );
            RsfsFileTest.assertFileContentsEqual( TEST_COMMON_DIRECTORY + "File2", "There".getBytes() );

            FileSystemImpl.resetStaticData();

            Enumeration directoryFileNames = fileSystem.listFilesInDirectory( TEST_COMMON_DIRECTORY );

            // We expect the directory listing to contain precisely the two subdirectories:
            //
            //  "file:////account1/folder1/email1/"
            //
            // and 
            // 
            //  "file:////account1/folder1/email2/".
            //
            // along with the two files:
            //
            //  "file:////account1/folder1/File1"
            //
            // and 
            // 
            //  "file:////account1/folder1/File2".
            //

            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName1 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName2 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName3 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName4 = (String) directoryFileNames.nextElement();
            assertFalse( directoryFileNames.hasMoreElements() );

            String[] expectedResponses = { TEST_DIRECTORY1, TEST_DIRECTORY2, "file:////account1/folder1/File1", "file:////account1/folder1/File2" };

            String[] actualResponses = { fileName1, fileName2, fileName3, fileName4 };

            assertEqualIgnoringOrder( expectedResponses, actualResponses );
        }
        catch( Exception e )
        {
            fail( e.getMessage() );
        }
    }

    public void test6()
    {
        LOG.info( "testListFilesInHigherDirectoryAndSomeFilesImmediatelyUnderWithDelete" );

        try
        {
            FileSystem fileSystem = FileSystemImpl.getFileSystem();

            for( int i = 0; i < 5; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );
                fileSystem.saveFile( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 + i, dummyFile, true );
                RsfsFileTest.assertFileContentsEqual( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 + i, Integer.toString( i ).getBytes() );
            }

            for( int i = 5; i < 10; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );
                fileSystem.saveFile( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 + i, dummyFile, true );
                RsfsFileTest.assertFileContentsEqual( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 + i, Integer.toString( i ).getBytes() );
            }

            DummyFile dummyFile1 = new DummyFile( "Hello".getBytes(), "text" );
            fileSystem.saveFile( TEST_COMMON_DIRECTORY + "File1", dummyFile1, true );
            RsfsFileTest.assertFileContentsEqual( TEST_COMMON_DIRECTORY + "File1", "Hello".getBytes() );

            DummyFile dummyFile2 = new DummyFile( "There".getBytes(), "text" );
            fileSystem.saveFile( TEST_COMMON_DIRECTORY + "File2", dummyFile2, true );
            RsfsFileTest.assertFileContentsEqual( TEST_COMMON_DIRECTORY + "File2", "There".getBytes() );

            FileSystemImpl.resetStaticData();

            Enumeration directoryFileNames = fileSystem.listFilesInDirectory( TEST_COMMON_DIRECTORY );

            // We expect the directory listing to contain precisely the two subdirectories:
            //
            //  "file:////account1/folder1/email1/"
            //
            // and 
            // 
            //  "file:////account1/folder1/email2/".
            //
            // along with the two files:
            //
            //  "file:////account1/folder1/File1"
            //
            // and 
            // 
            //  "file:////account1/folder1/File2".
            //

            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName1 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName2 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName3 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName4 = (String) directoryFileNames.nextElement();
            assertFalse( directoryFileNames.hasMoreElements() );

            String[] expectedResponses = { TEST_DIRECTORY1, TEST_DIRECTORY2, "file:////account1/folder1/File1", "file:////account1/folder1/File2" };

            String[] actualResponses = { fileName1, fileName2, fileName3, fileName4 };

            assertEqualIgnoringOrder( expectedResponses, actualResponses );

            fileSystem.deleteFilesUnderDirectory( TEST_DIRECTORY1 );

            directoryFileNames = fileSystem.listFilesInDirectory( TEST_COMMON_DIRECTORY );

            // We expect the directory listing to contain precisely the single subdirectory:
            //
            //  "file:////account1/folder1/email2/".
            //
            // along with the two files:
            //
            //  "file:////account1/folder1/File1"
            //
            // and 
            // 
            //  "file:////account1/folder1/File2".
            //

            assertTrue( directoryFileNames.hasMoreElements() );
            fileName1 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            fileName2 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            fileName3 = (String) directoryFileNames.nextElement();
            assertFalse( directoryFileNames.hasMoreElements() );

            String[] afterDeletionExpectedResponses = { TEST_DIRECTORY2, "file:////account1/folder1/File1", "file:////account1/folder1/File2" };

            String[] afterDeletionActualResponses = { fileName1, fileName2, fileName3 };

            assertEqualIgnoringOrder( afterDeletionExpectedResponses, afterDeletionActualResponses );

            // Now ensure the deletion is properly saved to flash.
            FileSystemImpl.resetStaticData();

            directoryFileNames = fileSystem.listFilesInDirectory( TEST_COMMON_DIRECTORY );

            // We expect the directory listing to contain precisely the single subdirectory:
            //
            //  "file:////account1/folder1/email2/".
            //
            // along with the two files:
            //
            //  "file:////account1/folder1/File1"
            //
            // and 
            // 
            //  "file:////account1/folder1/File2".
            //

            assertTrue( directoryFileNames.hasMoreElements() );
            fileName1 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            fileName2 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            fileName3 = (String) directoryFileNames.nextElement();
            assertFalse( directoryFileNames.hasMoreElements() );

            assertEqualIgnoringOrder( afterDeletionExpectedResponses, afterDeletionActualResponses );

        }
        catch( Exception e )
        {
            fail( e.getMessage() );
        }
    }

    public void test7()
    {
        LOG.info( "testListFilesInHigherDirectoryAndSomeFilesImmediatelyUnderDelete" );

        try
        {
            FileSystem fileSystem = FileSystemImpl.getFileSystem();

            for( int i = 0; i < 5; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );
                fileSystem.saveFile( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 + i, dummyFile, true );
                RsfsFileTest.assertFileContentsEqual( TEST_DIRECTORY1 + TEST_FILEINDIRECTORY1 + i, Integer.toString( i ).getBytes() );
            }

            for( int i = 5; i < 10; ++i )
            {
                DummyFile dummyFile = new DummyFile( Integer.toString( i ).getBytes(), "text" );
                fileSystem.saveFile( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 + i, dummyFile, true );
                RsfsFileTest.assertFileContentsEqual( TEST_DIRECTORY2 + TEST_FILEINDIRECTORY2 + i, Integer.toString( i ).getBytes() );
            }

            DummyFile dummyFile1 = new DummyFile( "Hello".getBytes(), "text" );
            fileSystem.saveFile( TEST_COMMON_DIRECTORY + "File1", dummyFile1, true );
            RsfsFileTest.assertFileContentsEqual( TEST_COMMON_DIRECTORY + "File1", "Hello".getBytes() );

            DummyFile dummyFile2 = new DummyFile( "There".getBytes(), "text" );
            fileSystem.saveFile( TEST_COMMON_DIRECTORY + "File2", dummyFile2, true );
            RsfsFileTest.assertFileContentsEqual( TEST_COMMON_DIRECTORY + "File2", "There".getBytes() );

            DummyFile dummyFile3 = new DummyFile( "Sweet".getBytes(), "text" );
            fileSystem.saveFile( "file:////otherdir/" + "File2", dummyFile3, true );
            RsfsFileTest.assertFileContentsEqual( "file:////otherdir/" + "File2", "Sweet".getBytes() );

            FileSystemImpl.resetStaticData();

            Enumeration directoryFileNames = fileSystem.listFilesInDirectory( TEST_COMMON_DIRECTORY );

            // We expect the directory listing to contain precisely the two subdirectories:
            //
            //  "file:////account1/folder1/email1/"
            //
            // and 
            // 
            //  "file:////account1/folder1/email2/".
            //
            // along with the two files:
            //
            //  "file:////account1/folder1/File1"
            //
            // and 
            // 
            //  "file:////account1/folder1/File2".
            //

            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName1 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName2 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName3 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            String fileName4 = (String) directoryFileNames.nextElement();
            assertFalse( directoryFileNames.hasMoreElements() );

            String[] expectedResponses = { TEST_DIRECTORY1, TEST_DIRECTORY2, "file:////account1/folder1/File1", "file:////account1/folder1/File2" };

            String[] actualResponses = { fileName1, fileName2, fileName3, fileName4 };

            assertEqualIgnoringOrder( expectedResponses, actualResponses );

            fileSystem.deleteFilesUnderDirectory( TEST_DIRECTORY1 );

            Enumeration remainingUnderTest1 = fileSystem.listFilesInDirectory( TEST_DIRECTORY1 );
            assertFalse( remainingUnderTest1.hasMoreElements() );

            Enumeration remainingUnderTest2 = fileSystem.listFilesInDirectory( TEST_DIRECTORY2 );
            assertTrue( remainingUnderTest2.hasMoreElements() );

            directoryFileNames = fileSystem.listFilesInDirectory( TEST_COMMON_DIRECTORY );

            // We expect the directory listing to contain precisely the single subdirectory:
            //
            //  "file:////account1/folder1/email2/".
            //
            // along with the two files:
            //
            //  "file:////account1/folder1/File1"
            //
            // and 
            // 
            //  "file:////account1/folder1/File2".
            //

            assertTrue( directoryFileNames.hasMoreElements() );
            fileName1 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            fileName2 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            fileName3 = (String) directoryFileNames.nextElement();
            assertFalse( directoryFileNames.hasMoreElements() );

            String[] afterDeletionExpectedResponses = { TEST_DIRECTORY2, "file:////account1/folder1/File1", "file:////account1/folder1/File2" };

            String[] afterDeletionActualResponses = { fileName1, fileName2, fileName3 };

            assertEqualIgnoringOrder( afterDeletionExpectedResponses, afterDeletionActualResponses );

            // Now ensure the deletion is properly saved to flash.
            FileSystemImpl.resetStaticData();

            directoryFileNames = fileSystem.listFilesInDirectory( TEST_COMMON_DIRECTORY );

            // We expect the directory listing to contain precisely the single subdirectory:
            //
            //  "file:////account1/folder1/email2/".
            //
            // along with the two files:
            //
            //  "file:////account1/folder1/File1"
            //
            // and 
            // 
            //  "file:////account1/folder1/File2".
            //

            assertTrue( directoryFileNames.hasMoreElements() );
            fileName1 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            fileName2 = (String) directoryFileNames.nextElement();
            assertTrue( directoryFileNames.hasMoreElements() );
            fileName3 = (String) directoryFileNames.nextElement();
            assertFalse( directoryFileNames.hasMoreElements() );

            assertEqualIgnoringOrder( afterDeletionExpectedResponses, afterDeletionActualResponses );

            // Now delete a bit more.
            fileSystem.deleteFilesUnderDirectory( TEST_COMMON_DIRECTORY );

            remainingUnderTest1 = fileSystem.listFilesInDirectory( TEST_DIRECTORY1 );
            assertFalse( remainingUnderTest1.hasMoreElements() );

            remainingUnderTest2 = fileSystem.listFilesInDirectory( TEST_DIRECTORY2 );
            assertFalse( remainingUnderTest2.hasMoreElements() );

            Enumeration remaining = fileSystem.listFiles();
            assertTrue( remaining.hasMoreElements() );
            String remainingFileName = (String) remaining.nextElement();
            assertEquals( "file:////otherdir/" + "File2", remainingFileName );
            assertFalse( remaining.hasMoreElements() );

        }
        catch( Exception e )
        {
            fail( e.getMessage() );
        }
    }

    protected int getNumberOfImplementedTestMethods()
    {
        return 7;
    }
}
